using OWL.Rendering.HRP;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;

namespace Yoozoo.Gameplay.gta
{
    public class CameraShot : MonoBehaviour
    {
        private RenderTexture _renderTexture;
        private HCameraShotData cameraShotData;
        public RenderTexture texture
        {
            get { return _renderTexture; }
            set
            {
                _renderTexture = value;
                if (cameraShotData != null)
                {
                    cameraShotData._renderTexture = null;
                }
            }
        }

        public bool IsTextureReady
        {
            get => this._renderTexture != null;
        }
        
        public delegate void TextureChangeDelegate();
        public event TextureChangeDelegate OnTextureChange;

        public Camera TargetCamera;

        /// <summary>
        /// 是否截取UI，是的话PassEvent设为AfterRendering，否则设为BeforeRendering
        /// </summary>
        public bool IsCaptureUI;

        public void AddTextureChangeCallback(TextureChangeDelegate callFunc)
        {
            OnTextureChange += callFunc;
        }

        private void OnEnable()
        {
            clearTemporary(true);
            
            cameraShotData = HRenderSettingsManager.Get().GetData<HCameraShotData>();
            if (cameraShotData == null)
                return;
            cameraShotData.bShotFrame = true;
            cameraShotData.TargetCamera = TargetCamera;
            cameraShotData.renderPassEvent =
                IsCaptureUI ? RenderPassEvent.AfterRendering : RenderPassEvent.BeforeRendering;
            
            StartCoroutine(WaitForEndOfFrame());
        }

        void OnDestroy()
        {
            clearTemporary(true);
        }

        //等到当前帧结束
        IEnumerator WaitForEndOfFrame()
        {
            //等到帧结束
            yield return new WaitForEndOfFrame();
            //此时渲染帧已结束
            if(cameraShotData._renderTexture != null && cameraShotData._renderTexture.IsCreated())
            {
                _renderTexture = cameraShotData._renderTexture;
                //第一次拿到图之后，就关掉CameraShot 和 UEPostEffects， 
                //在关掉窗口后，销毁CameraShot，重新打开UEPostEffects；
                //在窗口大小有变化时，重启CameraShot
                enabled = false;
                OnTextureChange?.Invoke();
            }
        }
        
        public void clearTemporary(bool releaseTextureImmediately = false)
        {
            if (_renderTexture != null)
            {
                if (releaseTextureImmediately)
                {
                    RenderTexture.ReleaseTemporary(_renderTexture);
                    _renderTexture = null;
                }
                else
                    StartCoroutine(DelayClearTexture());
            }
        }

        public void CheckMaskRT()
        {
            if (_renderTexture != null && _renderTexture.IsCreated() == false)
            {
                clearTemporary(false);
                enabled = true;
            }
        }
        
        private IEnumerator DelayClearTexture()
        {
            yield return null;
            if (_renderTexture != null)
            {
                RenderTexture.ReleaseTemporary(_renderTexture);
                _renderTexture = null;   
            }
        }
    }
}
